<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Cdkey;
use app\common\model\Depot;
use app\common\model\Prizes;
use app\common\model\Record;
use think\Config;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    //获取抽奖列表
    public function getPrizes()
    {
        $model = new Prizes();
        $list = $model->where('activity_id',1)->where('type','1')->select();
        $this->success('ok',$list);
    }
    
    
    //分享逻辑
    public function share()
    {
        /// 流程 用户A进入抽奖界面，点击抽奖提示登录小程序，用户登录小程序后，进行抽奖
        /// 如果抽奖次数用完，进行分享生成小程序分享链接
        /// 用户B通过分享的链接进入主界面，进行抽奖，提示登录，登录后才可抽奖(此时未关注公众号)
        /// 如果用户B关注公众号，则用户A增加一次机会。
        ///
        /// 小程序拒绝常见情况：诱导用户进行分享
    }

    //生成key
    public function getKey()
    {
//        halt($this->authcode(123456789,'sherk'));
        halt($this->authcode('clDLJTJJQSQ6AIquFmGe125nuxF3PnwziPyWj','sherk',true));
    }
    public function authcode($string,$key='',$operation=false,$expiry=0){
        $ckey_length = 1;
        $key = md5($key ? $key : DEFAULT_KEYS);
        $keya = md5(substr($key, 0, 16));
        $keyb = md5(substr($key, 16, 16));
        $keyc = $ckey_length ? ($operation? substr($string, 0, $ckey_length):substr(md5(microtime()), -$ckey_length)) : '';
        $cryptkey = $keya.md5($keya.$keyc);
        $key_length = strlen($cryptkey);
        $string = $operation? base64_decode(substr($string, $ckey_length)) :
            sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
        $string_length = strlen($string);
        $result = '';
        $box = range(0, 255);
        $rndkey = array();
        for($i = 0; $i <= 255; $i++) {
            $rndkey[$i] = ord($cryptkey[$i % $key_length]);
        }
        for($j = $i = 0; $i < 256; $i++) {
            $j = ($j + $box[$i] + $rndkey[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        for($a = $j = $i = 0; $i < $string_length; $i++) {
            $a = ($a + 1) % 256;
            $j = ($j + $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
        }
        if($operation) {
            if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&
                substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
                return substr($result, 26);
            } else {
                return '';
            }
        } else {
            return $keyc.str_replace('=', '', base64_encode($result));
        }
    }
    //输出所有可兑换商品
    public function getGoodsList()
    {
        $model = new Prizes();
        $list = $model->where('activity_id','2')->order(['createtime'=>'desc'])->select();
        foreach ($list as &$v){
            $v['goods_name'] = Depot::where('id',$v['depot_id'])->value('name');
            $v['num'] = Cdkey::where('depot_id',$v['depot_id'])->where('is_use',0)->count('id');
        }

        $this->success('ok',$list);
    }
    //获取最新20条中奖记录
    public function getRecordList()
    {
        $list = Record::with(['user'=>function($query){
            $query->withField('nickname,id');
        }])->order('createtime desc')->limit(20)->select();

        $this->success('ok',$list);
    }
    //获取抽奖说明
    public function getSetteminfo()
    {
        $info = Config::get('site.shuom');
        $this->success('ok',$info);
    }
}
